<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href="<%=basePath%>">
    <style type="text/css">

    </style>
</head>
<body>
<div class="page-return">
    <div class="ops-form">
        <div class="page-head">
            <form class="form-horizontal tasi-form" method="post" id="params" action="">
                <div class="form-group">
                    <label class="col-sm-1 control-label">模型名称</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="modelName" id="modelName" value="">
                    </div>
                    <label class="col-sm-1 control-label">模型分类</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control" name="category" id="category" value="">
                    </div>
                    <div class="col-sm-3">
                        <button type="button" class="btn btn-info" onclick="" id="modelSearch">搜索</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div class="ops-table" >
        <caption>
            <div class="ops-edit">
                <button type="button" class="btn btn-xs btn-info" id="modelAddBtn">新增</button>
                <button type="button" class="btn btn-xs btn-danger" id="modelDeleteBtn"><i class="ace-icon fa fa-trash-o bigger-110" title="删除"></i></button>
            </div>
        </caption>
        <div class="ops-table-content">
            <table id="modelTable"></table>
        </div>
    </div>
</div>
</body>
<script>
    $(function(){
        var modelTable = $("#modelTable").bootstrapTable({ // 对应table标签的id
            url: "/workflow/model/list", // 获取表格数据的url
            queryParams: function (params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
                var paramOption = {
                    pageSize: params.limit, // 每页要显示的数据条数
                    pageNumber: (params.offset/params.limit)+1, // 每页显示数据的开始行号
                    sortName: params.sort, // 要排序的字段
                    searchText:'',//搜索内容
                    sortOrder: params.order, // 排序规则
                    dataId: $("#dataId").val() // 额外添加的参数
                }
                return paramOption;
            },
            columns: [
                {
                    checkbox: true // 显示一个勾选框
                },{
                    field: 'id',
                    title: 'id',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },{
                    field: 'name',
                    title: '模型名称',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },
                {
                    field: 'key',
                    title: '流程定义key',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },
                {
                    field: 'category',
                    title: '分类',
                    align: 'center',
                    valign: 'middle',
                    halign:'center'
                },{
                    field: 'createTime',
                    title: '创建时间',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                },{
                    field: 'lastUpdateTime',
                    title: '最后更新时间',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return new Date(value).format("yyyy-MM-dd hh:mm:ss");
                    }
                },
                {
                    field: 'version',
                    title: '版本',
                    align: 'center',
                    valign: 'middle',
                    halign:'center',
                    formatter: function (value, row, index) {
                        return "v"+value;
                    }
                }
                ,{
                    title: "操作",
                    width: 270, // 定义列的宽度，单位为像素px
                    formatter: function (value, row, index) {
                        return '<button class="btn btn-xs btn-info" onclick="editor(\'' + row.id + '\')">流程设计器</button> &nbsp;'
                                +'<button class="btn btn-xs btn-success" onclick="deployment(\'' + row.id + '\')">部署</button> &nbsp;'
                                +'<button class="btn btn-xs btn-warning" onclick="view(\'' + row.id + '\')">预览</button> &nbsp;'
                                +'<button class="btn btn-xs btn-info" onclick="exportXml(\'' + row.id + '\')">导出</button> &nbsp;'
                                +'<button class="btn btn-xs btn-danger" onclick="del(\'' + row.id + '\')"><i class="ace-icon fa fa-trash-o bigger-110" title="删除"></i></button>   ';
                    }
                }
            ]
        });
        $("#modelAddBtn").click(function () {
            layer.open({
                type: 2,
                title: '新增模型',
                shadeClose: true,
                shade: false,
                area: ['780px', '480px'],
                content: '/views/model/model_form.jsp'
            });
        });
        $("#modelDeleteBtn").click(function () {
            var objArray = modelTable.bootstrapTable('getSelections');
            if(objArray.length < 1){
                layer.msg('请选择至少一条记录进行删除', function(){});
                return false;
            }
            var idArray = [];
            for (i = 0; i < objArray.length; i++) {
                idArray[i] = objArray[i].id;
            }
            layer.confirm('你确定要删除', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type:"get",
                    url:'/workflow/model/delete/?idArray='+idArray.join(','),
                    dataType:"json",
                    success:function(data){
                        if(data.resCode == '200'){
                            parent.$("#modelTable").bootstrapTable('refresh',{silent: true});
                            parent.layer.msg(data.resMessage,{time:1200});
                            return true;
                        }else {
                            parent.layer.msg(data.resMessage);
                            return false;
                        }
                    },
                    error:function(data){
                        parent.layer.msg("错误："+data.resMessage);
                    }
                })
            }, function(){
                layer.msg('取消操作', {
                    time: 1500 //1.5s后自动关闭
                });
            });
        });
    })

    /**
     * 父子页面传值，在这里，是传递模型id
     * @param modelId
     */
    function setModelId(modelId){
        editor(modelId)
    }

    /**
     * 打开流程设计器
     * @param moduleId
     */
    function editor(moduleId){
        layer.open({
            type: 2,
            title: '模型编辑器',
            shadeClose: true,
            shade: false,
            area: ['1230px', '700px'],
            content: '/workflow/model/editor/?moduleId='+moduleId
        });
    }

    /**
     * 删除
     */
    function del(Id){
        layer.confirm('你确定要删除', {
            btn: ['确定','取消'] //按钮
        }, function(){
            var idArray = [];
            idArray[0] = Id;
            $.ajax({
                type:"get",
                url:'/workflow/model/delete/?idArray='+idArray.join(','),
                dataType:"json",
                success:function(data){
                    if(data.resCode == '200'){
                        parent.$("#modelTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg(data.resMessage,{time:1200});
                        return true;
                    }else {
                        parent.layer.msg(data.resMessage);
                        return false;
                    }
                },
                error:function(data){
                    parent.layer.msg("错误："+data.resMessage);
                }
            })
        }, function(){
            layer.msg('取消操作', {
                time: 1500 //1.5s后自动关闭
            });
        });
    }

    /**
     * 部署
     * @param modelId 模型id
     */
    function deployment(modelId) {
        $.ajax({
            type:"get",
            url:'/workflow/model/deployment',
            data: {id:modelId},
            dataType:"json",
            cache: false,
            success:function(data){
                if(data.resCode == '200'){
                    parent.$("#modelTable").bootstrapTable('refresh',{silent: true});
                    parent.layer.msg(data.resMessage,{time:1200});
                    return true;
                }else {
                    parent.layer.msg(data.resMessage);
                    return false;
                }
            },
            error:function(data){
                parent.layer.msg("错误："+data.resMessage);
            }
        })
    }

    /**
     * 预览
     * @param modelId
     */
    function view(modelId){
        layer.open({
            type: 2,
            title: '预览xml',
            shadeClose: true,
            shade: false,
            area: ['1000px', '608px'],
            content: '/workflow/model/view/?moduleId='+modelId
        });
    }

    /**
     * 导出xml文件
     * @param modelId 模型id
     */
    function exportXml(modelId) {
        $.ajax({
            type:"POST",
            url:'/workflow/model/canexport',
            data: {id:modelId},
            dataType:"json",
            cache: false,
            success:function(data){
                if(data.resCode == '200'){
                    if (data.resData="true"){
                        window.location.href='<%=basePath%>/workflow/model/export/?modelId='+modelId;
                    }else{
                        parent.$("#modelTable").bootstrapTable('refresh',{silent: true});
                        parent.layer.msg("导出失败! 检查下是否已经画正确的流程图了?",{time:1200});
                    }
                    return true;
                }else {
                    parent.layer.msg(data.resMessage);
                    return false;
                }
            },
            error:function(data){
                parent.layer.msg("错误："+data.resMessage);
            }
        })
    }
    //搜索按钮
    $("#modelSearch").click(function () {
        $("#modelTable").bootstrapTable('refresh',{
            silent: true,
            query: {
                modelName:$("#modelName").val(),
                category:$("#category").val(),
            }
        });
    });
</script>
</html>